<!--
Theme Name: Kelontong Free
Theme URI: http://www.icreativelabs.com/
Description: None.
Version: 1.0.0
Author: iCreative Labs
Author URI: http://www.icreativelabs.com
Tags: blue, black
	Kelontong Free v1.0.0
	http://www.icreativelabs.com/
	This theme was designed and built by None,
	whose blog you will find at http://www.icreativelabs.com/
	The CSS, XHTML and design is released under GPL:
	http://www.opensource.org/licenses/gpl-license.php
-->
<?php
global $icl_utility, $wpsc_query, $wpdb, $query_string;

$query_string = '&'.$query_string;

$category = str_replace('/', '', $icl_utility->stripNonText($icl_utility->extractArgument($query_string, 'category_id')));
$page = $icl_utility->stripNonText($_GET['page']);

$current_category = (isset($category) && $category != '') ? $category : 'all';
$current_category_nicename = $icl_utility->get_category_nicename($category, TRUE);
$current_page = (isset($page) && $page != '') ? $page : 1;

$max_per_page_category = 4;
$max_per_page_product = 12;

if ($current_category == 'all')
{
	$product_count = count($icl_utility->get_categories('', '', FALSE));
	
	$categories = $icl_utility->get_categories((($current_page - 1) * $max_per_page_category), $max_per_page_category, FALSE);
	
	$max_page = ceil($product_count / $max_per_page_category);
	$max_page = ($max_page == 0) ? 1 : $max_page;
}
else
{
	$categories = array($icl_utility->get_category($current_category));
}

?>
<div class="product-listing-top">
	<?php if ($current_category == 'all') : ?>
	<h3><?php _e('Products'); ?></h3>
	<?php endif; ?>
	<?php if ($category != '') : ?>
	<h3><?php echo $categories[0]['name']; ?></h3>
	<?php endif; ?>
	<div class="listing-top-right">
		<p><?php _e('Page'); ?> <?php echo $current_page ?> <?php _e('of'); ?> <label id="sys_max_number_page"><?php echo $max_page ?></label></p>
		<p>
			<?php if ($current_page == 1) : ?>
			<label>< <?php _e('Previous'); ?></label>
			<?php else: ?>
			<a href="<?php echo get_option('siteurl'); ?>/products-page/<?php echo (($current_category != '' && $current_category != 'all') ? $current_category_nicename . '/' : ''); ?>?page=<?php echo ($current_page - 1); ?>">< <?php _e('Previous'); ?></a>
			<?php endif; ?>
			|
			<label id="sys_label_next_page" style="<?php if ($current_page == $max_page){}else{ echo 'display:none;'; } ?>"><?php _e('Next'); ?> ></label>
			<a id="sys_link_next_page" style="<?php if ($current_page == $max_page){ echo 'display:none;'; } ?>" href="<?php echo get_option('siteurl'); ?>/products-page/<?php echo (($current_category != '' && $current_category != 'all') ? $current_category_nicename . '/' : ''); ?>?page=<?php echo ($current_page + 1); ?>"><?php _e('Next'); ?> ></a>
		</p>
	</div><!-- .listing-top-right -->
	<div class="clear"></div>
</div><!-- .product-listing-top -->
<?php if (is_array($categories) && count($categories) > 0) : ?>
	<?php foreach($categories as $category) : ?>
		<?php if ($current_category == 'all') : ?>
		<h4>
			<?php echo $category['name']; ?>
			<a href="<?php echo get_option('siteurl'); ?>/products-page/<?php echo $icl_utility->get_category_nicename($category['id'], TRUE); ?>/"><?php _e('View All'); ?> ></a>
		</h4>
		<?php endif; ?>
		<ul class="product-listing">
			<li class="clear"></li>
			<?php
				if ($current_category != 'all')
				{
					$sql = $icl_utility->get_products_by_category_sql($category['id'], (($current_page - 1) * $max_per_page_product), $max_per_page_product);
					
					$product_count = count($wpdb->get_results($icl_utility->get_products_by_category_sql($category['id']), ARRAY_A));
					
					$max_page = ceil($product_count / $max_per_page_product);
					$max_page = ($max_page == 0) ? 1 : $max_page;
				}
				else
				{
					$sql = $icl_utility->get_products_by_category_sql($category['id'], 0, $max_per_row);
				}
				
				$wpsc_query->products = $wpdb->get_results($sql, ARRAY_A);
				$wpsc_query->product_count = count($wpsc_query->products);
			?>
			<?php while (wpsc_have_products()) :  wpsc_the_product(); ?>
			<li class="product-li">
				<div class="list-image">
				<?php if(get_option('show_thumbnails')) :?>
				<?php $product_image = wpsc_the_product_image(); ?>
				<?php if($product_image != '') :?>
					<a href="<?php echo wpsc_the_product_permalink(); ?>">
						<img id="product_image_<?php echo wpsc_the_product_id(); ?>" alt="<?php echo wpsc_the_product_title(); ?>" title="<?php echo wpsc_the_product_title(); ?>" src="<?php bloginfo('stylesheet_directory'); ?>/libraries/timthumb.php?w=145&src=<?php echo urlencode($product_image); ?>" />
					</a>
				<?php else: ?>
					<span>No Image Available</span>
				<?php endif; ?>
				<?php endif; ?>
				</div><!-- .list-image -->
				<div class="list-desc">
					<h5>
						<?php if(get_option('hide_name_link') == 1) : ?>
							<span><?php echo wpsc_the_product_title(); ?></span>
						<?php else: ?> 
							<a href="<?php echo wpsc_the_product_permalink(); ?>"><?php echo wpsc_the_product_title(); ?></a>
						<?php endif; ?>
					</h5>
					<p class="list-category">
						<?php echo $icl_utility->output_get_categories(wpsc_the_product_id(), ', ', '<a href="%s">', '</a>'); ?>
					</p><!-- .list-category -->
					<p class="list-price">
						<?php if(wpsc_product_is_donation()) : ?>
						<?php else : ?>
							<?php if(wpsc_product_on_special()) : ?>
								<span style="text-decoration:line-through;"><?php echo wpsc_product_normal_price(); ?></span>
							<?php endif; ?>
							<?php echo wpsc_the_product_price(); ?>
						<?php endif; ?>
					</p><!-- .list-price -->
					<p class="list-description">
						<?php echo wpsc_the_product_description(); ?>
					</p><!-- .list-description -->
					<div class="more">
						<a href="<?php echo wpsc_the_product_permalink(); ?>"><?php _e('More Information'); ?></a>
					</div><!-- .more -->
				</div><!-- .list-desc -->
			</li><!-- .product-li -->
			<?php endwhile; ?>
			<li class="clear"></li>
		</ul><!-- .product-listing -->
	<?php endforeach; ?>
<?php endif; ?>
<?php if ($current_category != 'all') : ?>
<script type="text/javascript">
	jQuery(document).ready(function(){
		jQuery('#sys_max_number_page').text(<?php echo $max_page; ?>);
		<?php if ($current_page == $max_page) : ?>
		jQuery('#sys_link_next_page').hide();
		jQuery('#sys_label_next_page').show();
		<?php endif; ?>
	});
</script>
<?php endif; ?>
<div class="product-listing-bottom">
	<div class="listing-bottom-right">
		<p><?php _e('Page'); ?> <?php echo $current_page ?> <?php _e('of'); ?> <?php echo $max_page ?></p>
		<p>
			<?php if ($current_page == 1) : ?>
			<label>< <?php _e('Previous'); ?></label>
			<?php else: ?>
			<a href="<?php echo get_option('siteurl'); ?>/products-page/<?php echo (($current_category != '' && $current_category != 'all') ? $current_category_nicename . '/' : ''); ?>?page=<?php echo ($current_page - 1); ?>">< <?php _e('Previous'); ?></a>
			<?php endif; ?>
			|
			<?php if ($current_page == $max_page) : ?>
			<label><?php _e('Next'); ?> ></label>
			<?php else: ?>
			<a href="<?php echo get_option('siteurl'); ?>/products-page/<?php echo (($current_category != '' && $current_category != 'all') ? $current_category_nicename . '/' : ''); ?>?page=<?php echo ($current_page + 1); ?>"><?php _e('Next'); ?> ></a>
			<?php endif; ?>
		</p>
	</div><!-- .listing-bottom-right -->
	<div class="clear"></div>
</div><!-- .product-listing-bottom -->